<?
class ZPlus_Plugin_Acl
{
  public function __construct()
  {
    $this->acl = array();
  }

  public function add($acl, $value = 0)
  {
    $this->acl[$acl] = $value;
  }

  public function addArray(array $acl)
  {
    foreach ($acl as $v)
      $this->acl[$v] = 0;
  }

  public function addAssoc(array $acl)
  {
    foreach ($acl as $k => $v)
      $this->acl[$k] = $v;
  }

  public function del($acl)
  {
    // prepare arguments
    $argc = func_num_args();
    if (empty($argc)) return false;
    if ($argc == 1)
    {
      $acl = func_get_arg(0);
      if (!is_array($acl))
        $acl = array($acl);
    }
    else
    {
      $acl = func_get_args();
    }

    // del
    foreach ($acl as &$v)
    {
      unset($this->acl[$v]);
    }
  }

  public function get($acl, $def = false)
  {
    if (!isset($acl) || !isset($this->acl[$acl])) return $def;
    return $this->acl[$acl];
  }

  public function has($acl)
  {
    if (empty($acl)) return false;
    return isset($this->acl[$acl]);
  }

  public function hasSome()
  {
    // prepare arguments
    $argc = func_num_args();
    if (empty($argc)) return false;
    if ($argc == 1)
    {
      $acl = func_get_arg(0);
      if (!is_array($acl))
        $acl = array($acl);
    }
    else
    {
      $acl = func_get_args();
    }

    // check
    foreach ($acl as &$v)
    {
      if (isset($this->acl[$v]))
        return true;
    }
    return false;
  }

  public function hasAll()
  {
    // prepare arguments
    $argc = func_num_args();
    if (empty($argc)) return false;
    if ($argc == 1)
    {
      $acl = func_get_arg(0);
      if (!is_array($acl))
        $acl = array($acl);
    }
    else
    {
      $acl = func_get_args();
    }

    // check
    foreach ($acl as &$v)
    {
      if (!isset($this->acl[$v]))
        return false;
    }
    return true;
  }

  public function __toString()
  {
    $acl = array();
    foreach ($this->acl as $k => $v)
      $acl[] = empty($v) ? $k : "$k($v)";
    sort($acl);
    return implode(' ', $acl);
  }
}
